﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Infrastructure.HardCoding;
using LinQProject;
using ServiceProject.EntityService;
using ServiceProject.VirtualClasses;

public partial class controls_ucSelection : System.Web.UI.UserControl
{
    protected FarmacieDataClassesDataContext database = new FarmacieDataClassesDataContext();
    protected ProductService productService;
    //protected MaterialService materialService;
    //protected MaterialColourService matColourService;

    public IDictionary<string,SelectedProduct> Selection
    {
        get
        {
            if (Session["Selection"] == null)
                return new Dictionary<string, SelectedProduct>();
            return (IDictionary<string, SelectedProduct>) Session["Selection"];
        }
        set
        {
            Session["Selection"] = value;
            if (value != null)
            {
                lblTotal.Visible = (value.Count > 0);
                lblTotalVal.Visible = (value.Count > 0);
                lnkButton.Visible = (value.Count > 0);
            }
        }
    }
    
    public bool IsCos
    {
        get
        {
            if (hdnIsCos.Value != null)
                return Convert.ToBoolean(hdnIsCos.Value);
            return true;
        }
        set
        {
            hdnIsCos.Value=value.ToString();
        }
    }

    public bool IsForModify
    {
        get
        {
            if (hdnIsForModify.Value != null)
            {
                bool result;
                if (Boolean.TryParse(hdnIsForModify.Value, out result))
                    return result;
            }
            return false;
        }
        set
        {
            hdnIsForModify.Value = value.ToString();

        }
    }
    
    public controls_ucSelection()
    {
        productService = new ProductService(database);
        //materialService=new MaterialService(database);
        //matColourService=new MaterialColourService(database);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadRepeater();
    }

    public void LoadRepeater()
    {
        if (Selection.Count >0)
        {
            IList<SelectedProduct> listaSelectedProducts = GetSelectedProducts();
            rptSelection.DataSource = listaSelectedProducts;
            rptSelection.DataBind();
            if (listaSelectedProducts.Count>0)
            {
                lblTotal.Visible = true;
                lblTotalVal.Visible = true;
                lnkButton.Visible = (!IsForModify);
            }
            else
            {
                lblTotal.Visible = false;
                lblTotalVal.Visible = false;
                lnkButton.Visible = false;
            }

            decimal total = 0m;
            foreach (var product in listaSelectedProducts)
            {
               total += product.ProductPrice*product.ProductQuantity;
            }

            lblTotalVal.Text = String.Format("{0:#.#,00}", total);
         
        }
        //else
        //{
            //rptSelection.DataSource = new List<SelectedProduct>();
            //rptSelection.DataBind();
            //lblTotal.Visible = false;
            //lblTotalVal.Visible = false;
            //lnkButton.Visible = false;
        //}
        
    }

    private IList<SelectedProduct> GetSelectedProducts()
    {
        IList<SelectedProduct> listToReturn=new List<SelectedProduct>();
        foreach (var item in Selection.Values)
        {
           listToReturn.Add(item);
        }
        return listToReturn;
    }

    public string DisplayProductName(int productId)
     {
         Product prod = productService.GetProductById(productId);
         if (prod == null)
             return string.Empty;
         return prod.ProductName;
     }

    protected object DisplayMaterialName(int materialId)
    {
        //Material mat = materialService.GetMaterialById(materialId, true);
        //if (mat == null)
        //    return string.Empty;
        //return mat.MaterialName;
        return string.Empty;
    }

    protected object DisplayColourName(int matColourId)
    {
        //MaterialColour matColour = matColourService.GetMaterialColour(matColourId);
        //if (matColour == null)
        //    return string.Empty;
        //return matColour.MaterialColourName;
        return string.Empty;
    }

    protected void lnkButton_Click(object sender, EventArgs e)
    {
        string DestinationUrl = String.Format("~/Order.aspx?OrderType={0}",
                                              IsCos ? ((int)Enums.orderType.order).ToString() 
                                              : ((int)Enums.orderType.request).ToString());
        Response.Redirect(DestinationUrl);

    }

    protected void lnkButtonM_Click(object sender, EventArgs e)
    {
        LinkButton lnk = (LinkButton) sender;
        string arg = lnk.CommandArgument;
        string [] split = arg.Split(new[] {'/'});

        int productId = Convert.ToInt32(split[0]);
        decimal productPrice = Convert.ToDecimal(split[3]);
        int productQuantity = Convert.ToInt32(split[4]);
        
        OnSelect(sender, new ProductSelectEventArgs(productId,productPrice,productQuantity));
    }

    protected void lnkBtnRemove_Click(object sender, EventArgs e)
    {
        LinkButton lnk = (LinkButton)sender;
        string arg = lnk.CommandArgument;
        string[] split = arg.Split(new[] { '/' });

        int productId = Convert.ToInt32(split[0]);
       decimal productPrice = Convert.ToDecimal(split[3]);
        int productQuantity = Convert.ToInt32(split[4]);

        OnDelete(sender, new ProductSelectEventArgs(productId, productPrice, productQuantity));    
    }
    
   #region Delegates && Events
	public delegate void ProductSelectEventHandler ( object sender, ProductSelectEventArgs e );
	
    public event ProductSelectEventHandler Select;
    protected void OnSelect ( object sender, ProductSelectEventArgs e )
	{
		if (Select != null) Select ( sender, e );
	}

    public event ProductSelectEventHandler Delete;
    protected void OnDelete(object sender, ProductSelectEventArgs e)
    {
        if (Delete != null) Delete(sender, e);
    }
    #   endregion
}

public class ProductSelectEventArgs : EventArgs
{
    public int ProductId { get; set; }

   public decimal ProductPrice { get; set; }

    public int ProductQuantity { get; set; }

    public ProductSelectEventArgs(int productId, decimal productPrice, int productQuantity)
    {
        ProductId = productId;
        ProductPrice = productPrice;
        ProductQuantity = productQuantity;
    }
}

